<template>
   <div style="height:100%;">
      <div>
        <div class='content-title'>
          <div class='content-title-child'>航班管理</div>
        </div>
        <hr class='content-hr'/>
      </div>
      <!--数据筛选条件+按钮-->
      <el-row class='hotlineClass' style="margin-top:10px">
        <el-upload
          class="upload-demo"
          ref="uploadExc"
          style="float:right;margin-right:20px"
          action=""
          :limit = "1"
          :http-request="uploadAirplaneFro"
          :on-progress="onProgress"
          :before-upload = "beforeUploadExc"
          list-type="picture"
          accept="excel"
          :show-file-list="false">
          <el-button type="warning">批量导入</el-button>
        </el-upload>
      </el-row>
      <!--表格数据-->
      <el-row style="margin-top:10px">
        <el-table
          v-loading="loading"
          :element-loading-text="loadingText"
          element-loading-spinner="el-icon-loading"
          :data="filterFormFro.tableData" 
          :border="true"
          :row-class-name="tableRowClassName"
          :header-cell-class-name = "tableHeaderClassName"
          v-bind:style="{width: tableWidth + 'px'}">
            <el-table-column prop="index" label="序号" min-width="15%">
            </el-table-column>
            <el-table-column prop="flightNumber" label="航班号" sortable min-width="20%">
            </el-table-column>
            <el-table-column prop="startDestination" label="出发到达" min-width="25%">
            </el-table-column>
            <el-table-column prop="departureTime" label="计划出发时间" min-width="30%">
            </el-table-column>
            <el-table-column prop="gate" label="登机口" min-width="10%">
            </el-table-column>
        </el-table>
      </el-row>
      <!--表格下方分页-->
      <el-row>
        <div class="pagination" style="text-align:center" v-show="paginationVisible">
          <el-pagination
            @current-change="handleCurrentChange"
            background
            :page-size="15"
            :current-page="filterForm.pageNum"
            layout="total, prev, pager, next"
            :page-count="filterFormFro.allPages">
          </el-pagination>
        </div>
      </el-row>
    </div>
</template>
<script>
import apiFile from '../../api/apiFile/apiFile.js'
import apiBus from '../../api/businessPlat/apiBus.js'
import '../../../static/css/businessPlat/common.css'
import '../../../static/css/businessPlat/hotline.css'
export default {
  created () {
    this.getFlightFro()
  },
  data () {
    return {
      airportNameList: [],
      filterForm: { // 筛选表单
        airportId: '',
        pageNum: 1,
        pageSize: 20
      },
      filterFormFro: {
        tableData: [],
        allPages: 1
      },
      tableWidth: '', // 表格宽度
      loadingText: '正在拼命加载中，请稍等......',
      loading: true,
      paginationVisible: false, // 表格下方的分页显示
      noteVisible: false,
      formLabelWidth: '170px',
      Note: '', // 提交提示
      pointVisible: false, // 加载弹窗是否显示
      addForm: { // 增加热线表单
        sortNum: '',
        hotLineName: '',
        airportId: '',
        hotLineNum: ''
      },
      addVisable: false,
      editVisable: false,
      editForm: { // 编辑热线表单
        sortNum: 0,
        hotLineName: '',
        airportId: '',
        hotLineNum: 0
      },
      deleteForm: {
        hotLineName: '',
        hotLineNum: '',
        sortNum: ''
      }
    }
  },
  methods: {
    // 分页页面跳转时
    handleCurrentChange (val) {
      this.filterForm.pageNum = val
      this.getFlightFro()
    },
    tableHeaderClassName ({row, column, rowIndex, columnIndex}) {
      if (rowIndex === 0) {
        return 'headerClass'
      }
    },
    tableRowClassName ({row, rowIndex}) {
      return 'rowClass'
    },
    // 点击表格中的“编辑”按钮
    handleEdit (index, row) {
      this.editVisable = true
      let data = this.filterFormFro.tableData[index]
      this.editForm.hotLineName = data.hotLineName
      this.editForm.hotLineNum = data.hotLineNum
      this.editForm.sortNum = data.sortNum
      this.editForm.airportId = data.airportId
      this.editForm.hotLineId = data.hotLineId
    },
    // 点击表格中的“删除”按钮
    handleDelete (index, row) {
      let data = this.filterFormFro.tableData[index]
      this.deleteForm.hotLineId = data.hotLineId
      this.$confirm('确定删除该热线信息吗?', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(() => {
        this.deleteHotLintFor()
      }).catch(() => {
        this.$message({
          type: 'info',
          message: '已取消删除'
        })
      })
    },
    cancleEdit (formName) {
      this.resetError(formName)
      this.editVisable = false
    },
    // 重置表单
    resetError (formName) {
      this.$nextTick(function () {
        this.$refs[formName].resetFields()
      })
    },
    setDeleteFro () {
      this.noteVisible = false
      this.$message.success('热线<' + this.deleteForm.hotLineName + '>已删除')
    },
    // 重置表单
    resetForm (formName) {
      this.$nextTick(function () {
        this.$refs[formName].resetFields()
      })
    },
    // 增加热线，点击“保存”
    submitAdd () {
      this.$refs['addForm'].validate((valid) => {
        if (valid) {
          this.addHotLintFor()
        } else {
          return false
        }
      })
    },
    // 编辑热线信息，点击“保存”
    submitEdit () {
      this.$refs['editForm'].validate((valid) => {
        if (valid) {
          this.editHotLintFor()
        } else {
          return false
        }
      })
    },
    // excel文件上传前进行检验格式
    beforeUploadExc (file) {
      const isExcel = (file.type === 'application/vnd.ms-excel' || file.type === 'application/vnd.openxmlformats-officedocument.spreadsheetml.sheet')
      if (!isExcel) {
        this.$message.warning('批量导入文件应为excel')
      }
      return isExcel
      // const isExcel = file.type === 'application/vnd.openxmlformats-officedocument.spreadsheetml.sheet'
    },
    // 文件上传时
    onProgress (event, file, fileList) {
      this.loading = true
    },
    /** ***************************** 向后台发送请求 ***************************************** */
    // 向后台发送信息批量导入航班信息(excel)
    uploadAirplaneFro () {
      this.loading = true
      let file = this.$refs['uploadExc'].uploadFiles[0].raw
      let para = new FormData()
      para.append('header', JSON.stringify({
        'token': sessionStorage.getItem('token')
      }))
      para.append('body', JSON.stringify({}))
      para.append('file', file)
      apiFile.addImportAirplane(para).then(res => {
        this.$refs['uploadExc'].clearFiles()
        this.loading = false
        if (res.data.body.result === 1000) {
          this.$message.success('批量导入航班信息成功！')
          this.getFlightFro()
        } else {
          this.$message.error(res.data.body.resultDesp)
        }
      })
      .catch(error => {
        this.$refs['uploadExc'].clearFiles()
        this.loading = false
        this.$message.error(error)
      })
    },
    // 向后台请求航班列表
    getFlightFro () {
      this.loading = true
      this.paginationVisible = false
      let para = {
        header: JSON.stringify({
          token: sessionStorage.getItem('token')
        }),
        body: JSON.stringify({
          pageNum: parseInt(this.filterForm.pageNum)
        })
      }
      this.filterFormFro.tableData = []
      apiBus.getFlight(para).then(res => {
        this.loading = false
        if (res.data.body.result === 1000) {
          this.filterFormFro.tableData = res.data.body.content.list
          if (res.data.body.content.list.length > 0) {
            let resData = res.data.body.content.list
            for (let i = 0; i < resData.length; i++) {
              this.filterFormFro.tableData[i].index = (parseInt(this.filterForm.pageNum) - 1) * 15 + i + 1
              this.filterFormFro.tableData[i].startDestination = resData[i].start + '->' + resData[i].destination
            }
            this.filterFormFro.allPages = res.data.body.content.allPages
            this.filterFormFro.allPages > 1 ? this.paginationVisible = true : this.paginationVisible = false
            this.$message.success('成功提取航班信息！')
          } else {
            this.$message.warning('没有任何航班信息')
          }
        } else {
          this.$message.error(res.data.body.resultDesp)
        }
      })
      .catch(error => {
        this.loading = false
        this.$message.error(error)
      })
    }
  },
  mounted () {
    this.tableWidth = document.body.scrollWidth - 259 - 20
  }
}
</script>